package com.vic.datastructure.leetcode.p5;

import java.util.*;

/**
 *
 */
public class Test {

    public static void main(String[] args) {

        int [] nums = {2,7,11,15};

        int target = 9;

        twoSum1(nums, target);

    }


    public static void twoSum1(int [] nums, int target) {
        int [] result = new int[2];
        for (int i = 0; i < nums.length; i++) {

            for (int j = i + 1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target) {
                    result[0] = i;
                    result[1] = j;
                }
            }

        }
        System.out.println(Arrays.toString(result));
    }


    public void twoSum2(int [] nums, int target) {
        Map<Integer,Integer> storeNums = new HashMap<>();

        int [] result = new int[2];

        for (int i = 0; i < nums.length; i++) {
            int another =  target - nums[i];

            Integer anotherIndex = storeNums.get(another);
            if(anotherIndex != null) {
                result[0] = i;
                result[1] = anotherIndex;
                break;
            } else {
                storeNums.put(nums[i], i);
            }

        }
        System.out.println(Arrays.toString(result));

    }

}
